<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" href="/static/pengpai/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" sec:authorize="hasAuthority('sys:permission:create')">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" sec:authorize="hasAuthority('sys:permission:delete')">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" name="编辑" sec:authorize="hasAuthority('sys:permission:update')"><i class="layui-icon layui-icon-edit">编辑</i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" sec:authorize="hasAuthority('sys:permission:delete')"><i class="layui-icon layui-icon-delete">删除</i></button>
</script>

<script type="text/html" id="power-type">
    {{#if (d.powerType == '0') { }}
    <span>目录</span>
    {{# }else if(d.powerType == '1'){ }}
    <span>菜单</span>
    {{# }else if(d.powerType == '2'){ }}
    <span>按钮</span>
    {{# } }}
</script>

<script type="text/html" id="power-enable">
    <input type="checkbox" name="enable" value="{{d.permissionId}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable"  {{ d.isEnabled== '1' ? 'checked' : '' }} />
</script>


<script src="/static/pengpai/component/layui/layui.js"></script>
<script src="/static/pengpai/component/pear/pear.js"></script>
<script>
    layui.use(['table','jquery','treetable', 'popup'],function () {
        let table = layui.table;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;

        window.render = function(){
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'permissionId',
                treePidName: 'parentId',
                skin:'line',
                treeDefaultClose: false,
                toolbar:'#power-toolbar',
                elem: '#power-table',
                url: '/sysPermission/list',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'permissionName', minWidth: 200, title: '权限名称'},
                        {field: 'permissionKey', minWidth: 200, title: '权限标识'},
                        {
                            field: 'permissionType', width: 150, align: 'center', templet: function (d) {
                                if (d.permissionType == 'B') {
                                    return '<span class="layui-badge layui-bg-gray">按钮</span>';
                                }
                                if (d.permissionType == 'N') {
                                    return '<span class="layui-badge layui-bg-blue">目录</span>';
                                } else {
                                    return '<span class="layui-badge layui-bg-green">菜单</span>';
                                }
                            }, title: '类型'
                        },
                        {field: 'href', title: '菜单url'},
                        {field: 'orderId', title: '排序'},
                        {title: '操作',templet: '#power-bar', width: 300, align: 'center'}
                    ]
                ]
            });
        }

        render();

        table.on('tool(power-table)',function(obj){
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })


        table.on('toolbar(power-table)', function(obj){
            if(obj.event === 'add'){
                window.add();
            } else if(obj.event === 'refresh'){
                window.refresh();
            } else if(obj.event === 'batchRemove'){
                window.batchRemove(obj);
            }  else if(obj.event === 'expandAll'){
                treetable.expandAll("#power-table");
            } else if(obj.event === 'foldAll'){
                treetable.foldAll("#power-table");
            }
        });

        window.add = function(){
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/sysPermission/add' + "?pid=1",
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }

        window.edit = function(obj){
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['100%', '100%'],
                content: '/sysPermission/edit?id='+obj.data.id,
            });
        }
        window.remove = function(obj){
            layer.confirm('确定要删除该权限', function(index){
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/sysPermission/del",
                    dataType:'json',
                    type:'post',
                    data: {"ids":obj.data['permissionId']},
                    success:function(result){
                        layer.close(loading);
                        if(result.code == 200){
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        }else{
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.batchRemove = function(obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据");
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].permissionId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些权限', {
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"/sysPermission/del",
                    dataType: 'json',
                    type: 'post',
                    data: {"ids":ids},
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            popup.success(result.msg, function () {
                                treetable.reload('#power-table');
                                // obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>